package com.gitee.wsl.ext.string


fun String.padding(char: String, block: Int, isAfter: Boolean = true) =
    chunked(block).joinToString("") {
        it.takeIf { it.length == block }
            ?: if (isAfter) {
                it + char.repeat(block - it.length)
            } else {
                char.repeat(block - it.length) + it
            }
    }

fun String.center(maxLength: Int, padding: String = " "): String {
    val len = properLength()
    if (len >= maxLength) {
        return this
    }
    val diff = maxLength - len
    val start = diff / 2
    val end = diff - start
    return padding.repeat(start) + this + padding.repeat(end)
}

fun String.limitLength(length: Int): String = if (this.length <= length) this else substring(0, length)

/**
 * Returns a string, of length at least `minLength`, consisting of `string` prepended
 * with as many copies of `padChar` as are necessary to reach that length. For example,
 *
 *
 *  * `padStart("7", 3, '0')` returns `"007"`
 *  * `padStart("2010", 3, '0')` returns `"2010"`
 *
 *
 *
 * See [java.util.Formatter] for a richer set of formatting capabilities.
 *
 * @param string the string which should appear at the end of the result
 * @param minLength the minimum length the resulting string must have. Can be zero or negative, in
 * which case the input string is always returned.
 * @param padChar the character to insert at the beginning of the result until the minimum length
 * is reached
 * @return the padded string
 */
/*fun String.padStart( minLength: Int, padChar: Char): String {
    if (length >= minLength) {
        return this
    }
    val sb = StringBuilder(minLength)
    for (i in length until minLength) {
        sb.append(padChar)
    }
    sb.append(this)
    return sb.toString()
}*/

/**
 * Returns a string, of length at least `minLength`, consisting of `string` appended
 * with as many copies of `padChar` as are necessary to reach that length. For example,
 *
 *
 *  * `padEnd("4.", 5, '0')` returns `"4.000"`
 *  * `padEnd("2010", 3, '!')` returns `"2010"`
 *
 *
 *
 * See [java.util.Formatter] for a richer set of formatting capabilities.
 *
 * @param string the string which should appear at the beginning of the result
 * @param minLength the minimum length the resulting string must have. Can be zero or negative, in
 * which case the input string is always returned.
 * @param padChar the character to append to the end of the result until the minimum length is
 * reached
 * @return the padded string
 */
/*
fun String.padEnd( minLength: Int, padChar: Char): String {

    if (length >= minLength) {
        return this
    }
    val sb = StringBuilder(minLength)
    sb.append(this)
    for (i in length until minLength) {
        sb.append(padChar)
    }
    return sb.toString()
}*/
